import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class PluginRefresh extends StatefulWidget {
  @override
  _PluginRefreshState createState() => _PluginRefreshState();
}

class _PluginRefreshState extends State<PluginRefresh> {
  RefreshController _controller = RefreshController();

  // 下拉刷新
  Future _onRefresh() async {
    await Future.delayed(Duration(milliseconds: 1500));
    _controller.refreshCompleted();
  }

  // 上拉加载
  Future _onLoading() async {
    await Future.delayed(Duration(milliseconds: 1500));
    // _controller.loadComplete();
    _controller.loadNoData();
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      controller: _controller,
      enablePullDown: true,
      onRefresh: _onRefresh,
      enablePullUp: true,
      onLoading: _onLoading,
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus? status) {
          Widget body;
          if (status == LoadStatus.idle) {
            body = Text('上拉加载');
          } else if (status == LoadStatus.loading) {
            body = CupertinoActivityIndicator();
          } else if (status == LoadStatus.failed) {
            body = Text('加载失败');
          } else if (status == LoadStatus.canLoading) {
            body = Text('释放加载');
          } else {
            body = Text('没有更多');
          }
          return Container(
            height: 55,
            alignment: Alignment.center,
            child: body,
          );
        },
      ),
      child: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text('item_$index'),
          );
        },
        itemCount: 30,
      ),
    );
  }
}
